home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / Tool Chest / Development Tools & Languages / • Other Platforms / PCCTS / testcpp / 1 / test.g < prev   
Encoding:
Text File  |  1994-09-14  |  2.0 KB  |  72 lines  |  [TEXT/MPS ]

  1. /* This is test.g which tests a simple DLG-based scanner */
  2.  
  3. /* ANTLR will assign token type numbers (by creating an enum which you
  4.  * get in tokens.h)
  5.  */
  6.  
  7. <<
  8. /* user must define ANTLRToken here */
  9. class ANTLRToken : public DLGBasedToken {
  10.    /* The base class we have chosen holds only a token type. */
  11.  
  12. protected:
  13.     /* For our simple purposes, a token and a string is enough in
  14.      * our attribute */
  15.     ANTLRChar _text[30];
  16.  
  17. public:
  18.     ANTLRToken(TokenType t, ANTLRChar *s) : DLGBasedToken(t) { ; }
  19.     /* Your derived class MUST have a blank constructor. */
  20.     ANTLRToken() {;}
  21.  
  22.     ANTLRChar *getText() { return _text; }
  23.     void setText(ANTLRChar *s) { strncpy(_text, s, 30); }
  24.  
  25.     /* WARNING WARNING WARNING: you must return a stream of distinct tokens */
  26.     virtual ANTLRLightweightToken *makeToken(TokenType tt, ANTLRChar *txt, int line)
  27.         {
  28.             ANTLRToken *t = new ANTLRToken;
  29.             t->setType(tt); t->setText(txt); t->setLine(line);
  30.             return t;
  31.         }
  32. };
  33.  
  34. /* "DLGLexer" must match what you use on DLG command line (-cl);
  35.  * "DLGLexer" is the default.
  36.  */
  37. #include "DLGLexer.h"        /* include definition of DLGLexer.
  38.                              * This cannot be generated automatically because
  39.                              * ANTLR has no idea what you will call this file
  40.                              * with the DLG command-line options.
  41.                              */
  42.  
  43. main()
  44. {
  45.     DLGFileInput in(stdin);    /* create an input stream for DLG to get chars from */
  46.     DLGLexer scan(&in);        /* create scanner reading from stdin */
  47.     ANTLRTokenBuffer pipe(&scan);/* make a buffered pipe between lexer & parser */
  48.     ANTLRToken aToken;        /* create a token to fill in for DLG */
  49.     scan.setToken(&aToken);
  50.     Expr parser(&pipe);        /* create a parser of type Expr hooked to the scanner */
  51.     parser.init();            /* init the parser; prime lookahead etc... */
  52.  
  53.     parser.e();                /* start parsing at rule 'e' of that parser */
  54. }
  55. >>
  56.  
  57. #token "[\ \t\n]+"    <<skip();>>
  58. #token Eof "@"
  59.  
  60. #tokclass My { IDENTIFIER NUMBER }
  61.  
  62. class Expr {                /* Define a grammar class */
  63.  
  64. e    :    My My Eof
  65.         <<fprintf(stderr, "text is %s,%s\n", $1->getText(), $2->getText());>>
  66.     ;
  67.  
  68. }
  69.  
  70. #token IDENTIFIER    "[a-z]+"
  71. #token NUMBER        "[0-9]+"
  72.